package com.swt.gap.scheduled;

import com.swt.gap.common.graph.GraphProcessor;
import com.swt.gap.dao.TaskDao;
import com.swt.gap.model.entity.Task;
import com.swt.gap.util.RedisUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.*;

/**
 * 定时任务，将图数据的边的数据保存至redis
 *
 * @author sx
 * @date 2020/7/28 20:08
 */
@Component
public class RedisGraphScheduledTask {

    private final TaskDao taskDao;
    private final RedisUtil redisUtil;
    private final GraphProcessor graphProcessor;

    public RedisGraphScheduledTask(TaskDao taskDao,
                                   RedisUtil redisUtil,
                                   GraphProcessor graphProcessor) {
        this.taskDao = taskDao;
        this.redisUtil = redisUtil;
        this.graphProcessor = graphProcessor;
    }

    @Scheduled(fixedRate = 30*60*1000)
    private void saveRelationshipIdToRedis() {
        ExecutorService executorService = new ThreadPoolExecutor(10, 10, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

        List<Task> tasks = taskDao.listAllActiveTask();
        tasks.forEach(task -> executorService.submit(new RedisTaskGraphRunnable(redisUtil, task.getTaskId(), graphProcessor)));
    }
}
